﻿<template>
	<view class="pageBox">
		<u-navbar title="限时秒杀" safeAreaInsetTop fixed placeholder>
			<view class="coreshop-navbar-left-slot" slot="left">
				<u-icon name="arrow-left" size="19" @click="goNavigateBack"></u-icon>
				<u-line direction="column" :hairline="false" length="16" margin="0 8px"></u-line>
				<u-icon name="home" size="22" @click="goHome"></u-icon>
			</view>
			<view slot="right">
			</view>
		</u-navbar>
		<view class="tab-box coreshop-flex coreshop-align-center">
			<view class="tab-item" @tap="onTab(tab.id,tab.status)" :class="{ 'tab-active': tabCurrent === tab.id }"
				v-for="tab in tabList" :key="tab.id">
				<text class="tab-title">{{ tab.title }}</text>
				<text v-show="tabCurrent === tab.id" class="tab-triangle"></text>
			</view>
		</view>
		<view class="content-box">
			<scroll-view scroll-y="true" enable-back-to-top @scrolltolower="loadMore" class="scroll-box">
				<view
					class="coreshop-flex-direction coreshop-padding-10 coreshop-margin-10 coreshop-bg-white coreshop-text-black"
					v-for="(item, key) in goodsList" :key="key" v-if="goodsList.length>0">
					<view class="coreshop-flex">
						<view>
							<u--image :src="item.goodThumbnail" mode="widthFix" width="96px" height="96px"></u--image>
						</view>
						<view class="coreshop-flex coreshop-flex-direction coreshop-padding-left-10 coreshop-percent-100">
							<view class="coreshop-font-15 u-line-2 coreshop-text-black">{{ item.name }}</view>
							<view
								class="coreshop-font-11 coreshop-padding-top-5 coreshop-padding-bottom-3 u-line-2 coreshop-text-brown">
								{{ item.goodName }}</view>
							<view class="coreshop-flex coreshop-align-center coreshop-padding-top-10  coreshop-padding-bottom-5">
								<view
									class="coreshop-font-11 coreshop-bg-orange coreshop-padding-2 coreshop-border-radius-4 coreshop-margin-right-10"
									v-if="item.maxNums>0">限购{{item.maxNums}}{{item.goodUnit}}</view>
								<view
									class="coreshop-font-11 coreshop-bg-green coreshop-padding-2 coreshop-border-radius-4 coreshop-margin-right-10"
									v-if="item.maxNums==0">不限购</view>
								<view
									class="coreshop-font-11 coreshop-bg-orange coreshop-padding-2 coreshop-border-radius-4 coreshop-margin-right-10"
									v-if="item.maxGoodsNums>0">总量{{item.maxGoodsNums}}{{item.goodUnit}}</view>
								<view
									class="coreshop-font-11 coreshop-bg-green coreshop-padding-2 coreshop-border-radius-4 coreshop-margin-right-10"
									v-if="item.maxGoodsNums==0">不限总量</view>

								<view class="coreshop-font-11 coreshop-bg-olive coreshop-padding-2 coreshop-border-radius-4">
									{{item.goodViewCount}}人浏览</view>
							</view>
							<view class="coreshop-flex coreshop-justify-between coreshop-flex-direction-row coreshop-align-center"
								v-if="item.timestamp">
								<view class="coreshop-text-right coreshop-time-right coreshop-margin-top-10"
									v-if="(item.startStatus == 1) && item.timestamp">
									<u-count-down :time="item.timestamp*1000" :autoStart="true" format="DD天HH时mm分ss秒"
										@change="onChange($event,key)">
										<view class="time coreshop-font-11">
											<view class="coreshop-font-12">仅剩：</view>
											<view class="time__custom">
												<text class="time__custom__item">{{ item.timeData.days }}</text>
											</view>
											<text class="time__doc">天</text>
											<view class="time__custom">
												<text
													class="time__custom__item">{{ item.timeData.hours >= 10 ? item.timeData.hours :'0' +item.timeData.hours}}</text>
											</view>
											<text class="time__doc">:</text>
											<view class="time__custom">
												<text class="time__custom__item">{{ item.timeData.minutes }}</text>
											</view>
											<text class="time__doc">:</text>
											<view class="time__custom">
												<text class="time__custom__item">{{ item.timeData.seconds }}</text>
											</view>
										</view>
									</u-count-down>
								</view>
								<view class="coreshop-text-right coreshop-time-right coreshop-margin-top-10"
									v-if="(item.startStatus == 0) && item.timestamp">
									<u-count-down :time="item.timestamp*1000" :autoStart="true" format="DD天HH时mm分ss秒"
										@change="onChange($event,key)">
										<view class="time">
											<view class="coreshop-font-12">即将开始：</view>
											<view class="time__custom">
												<text class="time__custom__item">{{ item.timeData.days }}</text>
											</view>
											<text class="time__doc">天</text>
											<view class="time__custom">
												<text
													class="time__custom__item">{{ item.timeData.hours>=10?item.timeData.hours:'0'+item.timeData.hours}}</text>
											</view>
											<text class="time__doc">:</text>
											<view class="time__custom">
												<text class="time__custom__item">{{ item.timeData.minutes }}</text>
											</view>
											<text class="time__doc">:</text>
											<view class="time__custom">
												<text class="time__custom__item">{{ item.timeData.seconds }}</text>
											</view>
										</view>
									</u-count-down>
								</view>
							</view>
							<view
								class="coreshop-flex coreshop-justify-between coreshop-flex-direction-row coreshop-align-center coreshop-margin-top-10">
								<view class="coreshop-flex coreshop-align-center">
									<view class="coreshop-font-14 coreshop-text-red">￥{{item.price}}</view>
									<view class="coreshop-font-xs coreshop-text-through coreshop-margin-left-5">{{item.mktPrice}}元</view>
								</view>
								<view class="coreshop-seckill-btn" v-if="item.startStatus == 1" @click="goSeckillDetail(item.id)">马上秒
								</view>
								<view class="coreshop-seckill-btn" v-if="item.startStatus == 0">即将开始</view>
								<view class="coreshop-buy-btn-disabled" v-if="item.startStatus == 2">已结束</view>
							</view>
						</view>
					</view>
					<!-- <view v-if="key+1 < listData.length">
						<view class="coreshop-divider">
							<view class="seckill"></view>
							<view class="dot">●</view>
							<view class="seckill"></view>
						</view>
					</view> -->
				</view>

				<!-- 无数据时默认显示 -->
				<view class="coreshop-emptybox" v-else>
					<u-empty :icon="$globalConstVars.apiFilesUrl+'/static/images/empty/data.png'" icon-size="150" text="暂无秒杀信息"
						mode="list"></u-empty>
				</view>
				<!-- 加载更多 -->
				<u-loadmore :status="loadStatus" :icon-type="iconType" :load-text="loadText" margin-top="20"
					margin-bottom="20" />
			</scroll-view>
		</view>
		<!-- 登录提示 -->
		<coreshop-login-modal></coreshop-login-modal>
	</view>
</template>

<script>
	export default {

		data() {
			return {
				loadStatus: 'loadmore',
				iconType: 'flower',
				loadText: {
					loadmore: '轻轻上拉',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				loading: false,
				page: 1,
				limit: 10,
				lastPage: 1,
				status: 1,
				tabCurrent: 'ing',
				goodsList: [],
				tabList: [{
						id: 'ing',
						title: '进行中',
						status: 1

					},
					{
						id: 'nostart',
						title: '即将开始',
						status: 0
					},
					{
						id: 'ended',
						title: '已结束',
						status: 2
					},
				]
			};
		},
		onLoad() {
			this.getGoodsList();
		},
		onReachBottom() {
			if (this.loadStatus === 'loadmore') {
				this.getGoodsList()
			}
		},
		methods: {
			onTab(id, status) {
				this.tabCurrent = id;
				this.status = status;
				this.goodsList = [];
				this.page = 1;
				this.loadStatus = 'loading';
				this.getGoodsList();
			},
			// 百分比
			getProgress(sales, stock) {
				let unit = '';
				if (stock + sales > 0) {
					let num = (sales / (sales + stock)) * 100;
					unit = num.toFixed(2) + '%';
				} else {
					unit = '0%';
				}
				return unit;
			},
			// 进度数
			getPercent(sales, stock) {
				let unit = 0;
				if (stock + sales > 0) {
					let num = (sales / (sales + stock)) * 100;
				} else {
					unit = 30;
				}
				return unit;
			},
			// 加载更多
			loadMore() {
				if (this.page < this.lastPage) {
					this.page += 1;
					this.getGoodsList();
				}
			},
			// 秒杀列表
			getGoodsList() {
				let _this = this;
				let data = {
					page: this.page,
					limit: this.limit,
					type: this.$globalConstVars.paymentType.seckill,
					status: this.status
				}
				this.loadStatus = 'loading';
				this.$u.api.getGroup(data).then(res => {
					if (res.status) {
						if (res.data) {
							let _goodsList = res.data.list;
							_this.goodsList = [..._this.goodsList, ..._goodsList]
						}
						_this.lastPage = res.data.totalPages;
						if (_this.page < res.data.totalPages) {
							_this.page++
							_this.loadStatus = 'loadmore'
						} else {
							_this.loadStatus = 'nomore'
						}
					} else {
						_this.$u.toast(res.msg)
					}
				});

			},
			onChange(e, key) {
				this.$set(this.goodsList[key], 'timeData', e)
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import "list.scss";
</style>